<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索与添加 -->
      <el-row>
        <el-col>
          <el-input placeholder="请输入内容"
                    v-model="searchText"
                    class="input-with-select"
                    clearable
                    @clear="clearSearch">
            <el-button slot="append"
                       icon="el-icon-search"
                       @click="searchGoods"></el-button>
          </el-input>

          <el-button type="primary"
                     @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- table栏 -->
      <el-table :data="goodsList"
                style="width: 100%"
                border>

        <el-table-column type="index"
                         width="50">

        </el-table-column>
        <el-table-column prop="goods_name"
                         label="商品名称">

        </el-table-column>
        <el-table-column prop="goods_price"
                         label="商品价格(元)"
                         width="180">

        </el-table-column>
        <el-table-column prop="goods_weight"
                         label="商品重量"
                         width="180">

        </el-table-column>
        <el-table-column label="创建日期"
                         width="200">
          <template slot-scope="scope">
            {{scope.row.add_time | format}}
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         width="180">
          <template slot-scope="scope">
            <el-button type="primary"
                       icon="el-icon-edit"
                       size="mini"
                       @click="editGoods(scope.row)"></el-button>
            <el-button type="danger"
                       icon="el-icon-delete"
                       size="mini"
                       @click="deleteGoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页功能-->
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="queryList.pagenum"
                     :page-sizes="[5, 10, 15, 20]"
                     :page-size="queryList.pagesize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>
    <!-- 修改对话框 -->
    <el-dialog title="修改商品信息"
               :visible.sync="EditDialogVisible"
               width="50%">
      <span>后端api有问题</span>
      <!-- <el-form :model="editInfo"
               :rules="editInfoRules"
               ref="editInfoRef"
               label-width="100px">
        <el-form-item label="商品名称"
                      prop="goods_name">
          <el-input v-model="editInfo.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格"
                      prop="goods_name">
          <el-input v-model="editInfo.goods_price"
                    type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品数量"
                      prop="goods_name">
          <el-input v-model="editInfo.goods_number"
                    type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量"
                      prop="goods_name">
          <el-input v-model="editInfo.goods_weight"
                    type="number"></el-input>
        </el-form-item>
      </el-form> -->
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="EditDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 搜索框绑定内容
      searchText: '',
      // 发出的请求数据
      queryList: {
        query: '',
        pagesize: 5,
        pagenum: 1
      },
      total: 0,
      // 获取到的商品列表
      goodsList: [],
      // 对话框的显示隐藏
      EditDialogVisible: false,
      // 当前修改的信息
      editInfo: {
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: ''
      },
      // 修改对话框的验证规则
      editInfoRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }]
      },
      editID: 0

    }
  },
  methods: {
    // 获取商品列表
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryList
      })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表失败')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 监听当前页发生变化
    handleSizeChange (pagesize) {
      this.queryList.pagesize = pagesize
      this.getGoodsList()
    },
    // 监听总条数变化
    handleCurrentChange (pagenum) {
      this.queryList.pagenum = pagenum
      this.getGoodsList()
    },
    // 查询商品
    searchGoods () {
      this.queryList.query = this.searchText
      this.queryList.pagenum = 1
      this.getGoodsList()
    },
    // 清空搜索
    clearSearch () {
      this.queryList.query = ''
      this.getGoodsList()
    },
    // 添加商品功能
    addGoods () {
      this.$router.push('/add')
    },
    // 修改商品信息
    editGoods (row) {
      console.log(row)

      this.EditDialogVisible = true
      this.editInfo.goods_name = row.goods_name
      this.editInfo.goods_price = row.goods_price
      this.editInfo.goods_number = row.goods_number
      this.editInfo.goods_weight = row.goods_weight
      this.editID = row.goods_id
    },
    // 添加修改
    addEdit () {
      // const { data: res } = await this.$http.put('goods/' + this.editID, this.editInfo)
      this.EditDialogVisible = false
    },
    async deleteGoods (row) {
      const confirm = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirm === 'cancel') {
        this.$message.info('已取消')
        return false
      }
      const { data: res } = await this.$http.delete('goods/' + row.goods_id)
      if (res.meta.status !== 200) return this.$message.error('删除商品失败')
      this.getGoodsList()
      this.$message.success('删除成功')
    }

  },
  created () {
    this.getGoodsList()
  },
  filters: {
    format: function (value) {
      var date = new Date(value)

      var map = {
        Y: date.getFullYear(), // 年份
        M: (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1, // 月份
        d: (date.getDate()) < 10 ? '0' + date.getDate() : date.getDate(), // 日
        h: (date.getHours()) < 10 ? '0' + date.getHours() : date.getHours(), // 小时
        m: (date.getMinutes()) < 10 ? '0' + date.getMinutes() : date.getMinutes(), // 分
        s: (date.getSeconds()) < 10 ? '0' + date.getSeconds() : date.getSeconds() // 秒

      }

      return `${map.Y}-${map.M}-${map.d} ${map.h}：${map.m}：${map.s}`
    }
  }
}

</script>

<style lang="less" scoped>
.el-input {
  width: 350px;
  margin-right: 20px;
}
.el-row {
  margin-bottom: 20px;
}
</style>
